package top.wangjiu.reactive.rpc.autoconfigure.server;

import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;

/**
 * @ClassName ReactiveRpcSpringApplicationRunListener
 * @Description TODO
 * @Author wangjiu
 * @Date 2021/8/10 8:08 下午
 */
public class ReactiveRpcSpringApplicationRunListener implements SpringApplicationRunListener {

    private final SpringApplication application;

    private final String[] args;

    public ReactiveRpcSpringApplicationRunListener(SpringApplication application,String[] args){
        this.application = application;
        this.args = args;
    }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        Class<?> mainApplicationClass = application.getMainApplicationClass();
        EnableRpcServer annotation = mainApplicationClass.getAnnotation(EnableRpcServer.class);
        if(annotation!=null){
            //替换web容器
            if(annotation.mainServer()){
                application.setApplicationContextFactory(webApplicationType -> new ReactiveRpcApplicationContext());
                application.setEnvironment(new ReactiveRpcApplicationEnvironment());
            }
        }
    }

}
